package com.OnTimeTeam.OnTime.Alert;


import com.OnTimeTeam.OnTime.AlarmFeatures;
import com.OnTimeTeam.OnTime.AlarmSetter;
import com.OnTimeTeam.OnTime.MainActivity;
import com.OnTimeTeam.OnTime.RemindersIO;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MyAlarmService extends Service {//The alarm service - Service does not has UI
	  	
	
    public int onStartCommand(Intent intent,int flags, int startId) {
    	
		super.onStartCommand(intent, flags, startId);
		
		String remindersPath = getApplicationContext().getFilesDir().getPath().toString() + "/reminders.xml";
		String alarmID = intent.getExtras().getString("id");
		String alarmSort = intent.getExtras().getString("sort");
		
		//Close MainActivity if open
        try{MainActivity.thisActivity.finish();} 
        catch(Exception e){}
		
		
		//If there is already one alarm open
		if((MyAlertFinal.isAlarmOn || MyAlert2.isAlarmOn)){
			
			//Change Snooze sign and Status
	        RemindersIO.changeSnoozeSign(alarmID, remindersPath, "None");
	        RemindersIO.changeStatusByID(alarmID, remindersPath, "Off");
	        
			//Cancel Pending alarm
			AlarmFeatures af = new AlarmFeatures();
			af.setDaysIDS(RemindersIO.getDaysIDS(alarmID, remindersPath));
			AlarmSetter.cancelExistingAlarm(af, getApplicationContext());
			
			
	        Log.e("AlarmTurnedOff","AlarmTurnedOff");
	        
			return Service.START_NOT_STICKY;
		}
		

		Intent alertIntent = null;
		
		//In case we have only 1 repeat left:
		if(intent.getExtras().getString("snoozeRepeat").equals("1")){
				
			//Set Snooze to NONE
			RemindersIO.changeSnoozeSign(alarmID, remindersPath, "None");
			
			//Set alarm INACTIVE
			if(alarmSort.equals("Quick")){
				
				RemindersIO.changeStatusByID(alarmID, remindersPath, "Off");
				
				//Cancel Pending alarm
				AlarmFeatures af = new AlarmFeatures();
				af.setDaysIDS(RemindersIO.getDaysIDS(alarmID, remindersPath));
				AlarmSetter.cancelExistingAlarm(af, getApplicationContext());
			}
	        
			//Set final alert activity
			alertIntent = new Intent(this , MyAlertFinal.class);
		}
		else { //Alarm left at least 2 repeats
			
			//Set alert activity
			alertIntent = new Intent(this , MyAlert2.class);
		}
		
		//Put all EXTRAS
		alertIntent.putExtra("snoozeRepeat", intent.getExtras().getString("snoozeRepeat"));
		alertIntent.putExtra("snoozeInterval", intent.getExtras().getString("snoozeInterval"));
		alertIntent.putExtra("tune", intent.getExtras().getString("tune"));
		alertIntent.putExtra("label", intent.getExtras().getString("label"));
		alertIntent.putExtra("vibration", intent.getExtras().getString("vibration"));
		alertIntent.putExtra("volume", intent.getExtras().getString("volume"));
		alertIntent.putExtra("id", intent.getExtras().getString("id"));
		alertIntent.putExtra("hour", intent.getExtras().getString("hour"));
		alertIntent.putExtra("minute", intent.getExtras().getString("minute"));
		alertIntent.putExtra("sort", intent.getExtras().getString("sort"));
		
		//Set the right flags for the intent
		alertIntent.setFlags(Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);

		//Print the extras
		printExtras(intent);
		
		//start alert on main thread
		startActivity(alertIntent);

		Log.e("CALLED ALERT", "CalledAlert from MyAlarmService");
		
		return Service.START_NOT_STICKY;
    }
    
    //Print stuff
    void printExtras(Intent intent) {
		//Print
		Log.e("snoozeRepeat", intent.getExtras().getString("snoozeRepeat"));
		Log.e("snoozeInterval", intent.getExtras().getString("snoozeInterval"));
		Log.e("tune", intent.getExtras().getString("tune"));
		Log.e("label", intent.getExtras().getString("label"));
		Log.e("vibration", intent.getExtras().getString("vibration"));
		Log.e("volume", intent.getExtras().getString("volume"));
		Log.e("id", intent.getExtras().getString("id"));
		Log.e("hour", intent.getExtras().getString("hour"));
		Log.e("minute", intent.getExtras().getString("minute"));
		Log.e("sort", intent.getExtras().getString("sort"));

    }

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	

}